HEAD ======= >>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
suppressPackageStartupMessages({
library(knitr)
library(pander)
library(dygraphs)
library(zoo)
library(Quandl)
library(ggplot2)
library(rbokeh)
})
Before writing your RMarkdown document, user can specify the global settings for the global environment. For example, setting whether to echo your R code and show the code/results etc. in the document. You can either set the options globally or for individual code blocks.
## global setting for all blocks
{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
## setting for individual code block
{r chuck_name, echo = FALSE, results = "hide", include = TRUE}
x <- rnorm(100)
y <- 2*x + rnorm(100)
If you want all the figures take the same amount of space, use the following commands:
{r global_options, include = FALSE}
knitr::opts_chunk$set(fig.width = 12, fig.height = 8, fig.path = 'Figs/',
echo = FALSE, warning = FALSE, message = FALSE)
# While setting figure path, the '/' is very import.
Also, you can individually change the settings for your figure windows. This will overwrite global settings. This is very convenient especially when you have images for different data size. For example, by specifying ‘fig.with’ and ‘fig.height’:
{r scatterplot, fig.width = 8, fig.height = 6}
plot(x,y)
# Header 1
## Header 2
### Header 3
* Item 1
* Item 2
+ Item 2a
+ Item 2b
This is the example output for Unordered Lists:
1. Item 1
2. Item 2
3. Item 3
+ Item 3a
+ Item 3b
This is the example output for ordered list:
From time to time, we might want to emphasize our text either using {**bold**} or {*italic*}. Also, we can change the text color by using styles. We can say roses are red, violets are blue by using command <span style="color:red">red</span> and <span style="color:blue">blue</span> repsectively.
R code will be evaluated and printed
size = 750
x = as.data.frame(matrix(runif(14*size),ncol=14))
head(x)
<<<<<<< HEAD
## V1 V2 V3 V4 V5 V6
## 1 0.7548207 0.2945704 0.18991178 0.2991356 0.542085034 0.8699078
## 2 0.1722386 0.5325214 0.94117480 0.5981589 0.662857883 0.1253510
## 3 0.7185763 0.8012025 0.73345663 0.2198048 0.005043637 0.8216537
## 4 0.7281209 0.7730873 0.30950364 0.2475588 0.848580285 0.5912194
## 5 0.2927274 0.1946505 0.77037892 0.4902956 0.053748523 0.2242255
## 6 0.5568808 0.8588699 0.08851095 0.6293152 0.338115964 0.5248185
## V7 V8 V9 V10 V11 V12 V13
## 1 0.96531414 0.6556900 0.4929799 0.4784724 0.9426973 0.003610311 0.2207848
## 2 0.32736569 0.3022840 0.4003558 0.3493958 0.2649559 0.746046715 0.8595444
## 3 0.77310659 0.8038141 0.9043817 0.2590332 0.6863734 0.125399736 0.2097843
## 4 0.09626997 0.3580368 0.3071267 0.7322447 0.2279688 0.877367509 0.2435542
## 5 0.42387970 0.5898082 0.7616771 0.6864669 0.9713130 0.340537479 0.7655595
## 6 0.81067242 0.3470121 0.6863067 0.7027534 0.7429288 0.272852706 0.2010344
## V14
## 1 0.4627359
## 2 0.8190717
## 3 0.8079272
## 4 0.2574419
## 5 0.3926048
## 6 0.1062768
=======
## V1 V2 V3 V4 V5 V6
## 1 0.33166033 0.1420655 0.2836498 0.75891100 0.31424165 0.697032094
## 2 0.82259151 0.9626836 0.2126412 0.15594293 0.96962867 0.627802575
## 3 0.78307095 0.4538515 0.6035929 0.63552651 0.87709911 0.094951871
## 4 0.61278501 0.2380527 0.2902754 0.04142245 0.33188859 0.389424942
## 5 0.43043430 0.8465630 0.6015905 0.06995226 0.89579034 0.334250760
## 6 0.01801764 0.6452595 0.5422810 0.36754360 0.07109973 0.005243477
## V7 V8 V9 V10 V11 V12 V13
## 1 0.367260975 0.3372149 0.2330494 0.1844045 0.2548702 0.13115498 0.0783551
## 2 0.315287754 0.3600626 0.7621619 0.3251440 0.9335521 0.56685419 0.9037180
## 3 0.105956119 0.9608078 0.6674662 0.7091772 0.8803608 0.63756200 0.4141541
## 4 0.726666056 0.1358177 0.8948132 0.9952181 0.8469392 0.07849199 0.7698287
## 5 0.006574286 0.6294320 0.5281297 0.1160245 0.6145946 0.64806517 0.9942196
## 6 0.338450101 0.9253563 0.1131822 0.1603369 0.8691608 0.50156408 0.4531857
## V14
## 1 0.5576812
## 2 0.6232648
## 3 0.7716351
## 4 0.2396432
## 5 0.4220654
## 6 0.8644235
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
In RMarkdown, we can symply embed a chunk of code by using
x = matrix(runif(14*size),ncol=14)
y = 10 * sin(pi*x[,1]*x[,2]) + 20*(x[,3]-0.5)**2 + 10*x[,4] + 5*x[,5] + runif(1,0,1)
x[,11:14] = x[,1:4] + matrix(runif(4*size,min = 0, max = 0.025),ncol=4)
Inline code can be excecuted using two plus two equals 4 using two plus two equals 4
data=data.frame(value=rnorm(10000))
# Basic histogram
ggplot(data, aes(x=value)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<<<<<<< HEAD
# Custom Binning. I can just give the size of the bin
ggplot(data, aes(x=value)) + geom_histogram(binwidth = 0.05)
# Uniform color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, color="white", fill=rgb(0.2,0.7,0.1,0.4) )
# Proportional color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, aes(fill = ..count..) )
# Custom Binning. I can just give the size of the bin
ggplot(data, aes(x=value)) + geom_histogram(binwidth = 0.05)
# Uniform color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, color="white", fill=rgb(0.2,0.7,0.1,0.4) )
# Proportional color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, aes(fill = ..count..) )
Images on the web or local files in the same directory:


IHS Markit
There are two ways to embed equations using LaTex format: inline equation and block equations.
This is an example of inline equation using $ quotes. $10sin(\pi x_1x_2)$ ==> \(10sin(\pi x_1x_2)\)
This is an example of display equations using $$ quotes. $$y = 10sin(\pi x_1x_2) + 20(x_3-0.5)^2 + 10x_4+5x_5+\epsilon$$=> \[y = 10sin(\pi x_1x_2) + 20(x_3-0.5)^2 + 10x_4+5x_5+\epsilon\]
R Markdown can also create simple tables very easily by following syntax:
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
Content Cell | Content Cell
Reference Style Links and Images
Here is an example output:
| Name | Age | Hobby |
|---|---|---|
| Jack | 22 | Travel |
| Adam | 33 | Movie |
| Judy | 25 | Party |
In RMarkdown, we can also easily cite our references by coupling with reference managers. For example: Application written in the R programming language [@RCoreTeam] using the Shiny framework [@Tutorialspoint2015].
RMarkdown can also embed advanced objects into its HTML output: video, interactive graphics etc. Here are some examples of the advanced features:
brent <- Quandl("EIA/PET_RBRTE_D", collapse="monthly", type="ts")
dygraph(brent, main = "Brent Spot Prices") %>% dyRangeSelector() %>%
dyAxis("y", label = "US $/Barrel") %>%
dySeries("V1", label = "Brent ($/Barrel)") %>%
dyLegend(show = "follow")
<<<<<<< HEAD
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=-77.003, lat=38.9, popup="IHS MARKIT")
m # Print the map
<<<<<<< HEAD
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
library(rbokeh)
figure() %>%
ly_points(Sepal.Length, Sepal.Width, data = iris,
color = Species, glyph = Species,
hover = list(Sepal.Length, Sepal.Width))
<<<<<<< HEAD
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
## Warning in structure(x, class = unique(c("AsIs", oldClass(x)))): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
## Consider 'structure(list(), *)' instead.
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
library(visNetwork)
nodes <- data.frame(id = 1:6, title = paste("node", 1:6),
shape = c("dot", "square"),
size = 10:15, color = c("blue", "red"))
edges <- data.frame(from = 1:5, to = c(5, 4, 6, 3, 3))
visNetwork(nodes, edges) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
<<<<<<< HEAD
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
geom_bar(position = "dodge")
ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
<<<<<<< HEAD
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
# Load data
library(networkD3)
##
## Attaching package: 'networkD3'
## The following object is masked from 'package:leaflet':
##
## JS
data(MisLinks)
data(MisNodes)
# Plot
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
<<<<<<< HEAD
=======
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
URL <- paste0(
"https://cdn.rawgit.com/christophergandrud/networkD3/",
"master/JSONdata//flare.json")
## Convert to list format
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)
# Use subset of data for more readable diagram
Flare$children = Flare$children[1:3]
radialNetwork(List = Flare, fontSize = 10, opacity = 0.9)
<<<<<<< HEAD
diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9)
hc <- hclust(dist(USArrests), "ave")
dendroNetwork(hc, height = 600)
=======
diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9)
hc <- hclust(dist(USArrests), "ave")
dendroNetwork(hc, height = 600)
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d
#htmltools::includeHTML("embed_video_Rmarkdown.html")
<<<<<<< HEAD
=======
R Core Team. 2016. “R: A Language and Envirnoment for Statistical Computing.” Journal Article. http:///www.R-project.org.
Tutorialspoint. 2015. “Perl Programming.”
>>>>>>> eb7e9f72e21ae9be1ba844417fccd9e82db6b18d